<?php
namespace Qwadmin\Controller;

use Vendor\Tree;
use Think\Model;

class UserController extends ComController{
	
	/**
	 * 前台用户管理
	 * */
	public function index($sid = 0, $p = 1)
	{
		//获得所有公众号平台
		$platform_list = D("platform")->select();
		$this->assign("platform_list",$platform_list);
		
// 		//查询条件
		if(!empty($_GET["nickname"])) {
			$map["nickname"] = array("like","%".$_GET["nickname"]."%");
			$this->assign("search_name",$_GET["nickname"]);
		}
		if(!empty($_GET["domain"])){
			$map["domain"] = $_GET["domain"];
			$this->assign("domain",$_GET["domain"]);
		}
// 		if(!empty($_GET["other_class_id"])){
// 			$map["other_class_id"] = $_GET["other_class_id"];
// 			$this->assign("search_other_class_id",$_GET["other_class_id"]);
// 		}
		$p = $_GET["p"] ? $_GET["p"] : 1;
		
		$novel = M('user');
		$pagesize = 20;#每页数量
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		
		
		$count = $novel->where($map)->count();
		$list = $novel->where($map)->order("user_id desc")->limit($offset . ',' . $pagesize)->select();
		foreach($list as $key=>$item){
			$rs = D("platform")->field("platform_name")->where("domain='".$item["domain"]."'")->find();
			$list[$key]["platform_name"] = $rs["platform_name"];
			
			//获得历史记录
			$map["open_id"] = $item["open_id"];
			$hi_rs = D("read_history")->where($map)->order($map)->order("addtime desc")->find();
			if(!empty($hi_rs))
				$list[$key]["history"] = $hi_rs["novel_name"]." ".$hi_rs["chapter"]." <a href='/Home/Novel/show/id/".$hi_rs["chapter_id"]."/novel_id/".$hi_rs["novel_id"]."'>".$hi_rs["chapter_name"]."</a>";
		}
		
		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();
		$this->assign('list', $list);
		$this->assign('page', $page);
		$this->display();
	}
	
	/**
	 * 
	 * */
	public function send_info(){
		set_time_limit(0);
		$user_id = $_POST["user_ids"];
		if(!empty($user_id)){
			$map["user_id"] = array("in", $user_id);
			$list = D("user")->where($map)->select();
			foreach ($list as $key=>$item){
				$this->sendimg($item["domain"], "@".$item["nickname"]."  ".$_POST["title"], $_POST["desc"], $_POST["url"], $_POST["img"], $item["open_id"]);
			}
		}
		else{
			$list = D("user")->select();
			foreach ($list as $key=>$item){
				$this->sendimg($item["domain"], "@".$item["nickname"]."  ".$_POST["title"], $_POST["desc"], $_POST["url"], $_POST["img"], $item["open_id"]);
			}
		}
		
		echo "the end";
	}
	
	/**
	 * 群发图文接口
	 * @domain 用户针对的域名,判断用户是哪个公众号的
	 * @title 图文标题
	 * @description 描述
	 * @url 连接地址
	 * @picurl 图片地址
	 * @open_id 用户OPEN_ID
	 * */
	protected function sendimg($domain,$title,$description,$_url,$picurl,$open_id){
		$map["domain"] = $domain;		
		$platform = D("platform")->where($map)->find();
		if(intval($platform["out_time"]) > time())	{
			$token = getWeChatToken($platform["appid"],$platform["secret"],$platform["token"]);
		}
		else{
			$token = getWeChatToken($platform["appid"],$platform["secret"]);
		}
		
		$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$token";
		$data = '{
				    "touser":"'.$open_id.'",
				    "msgtype":"news",
				    "news":{
				        "articles": [
					         {
					             "title":"'.$title.'",
					             "description":"'.$description.'",
					             "url":"'.$_url.'",
					             "picurl":"'.$picurl.'"
					         }
				         ]
				    }
				}
		';
		$result = weChatCurl($url, 1, $data);
	}
	
	/**
	 * 遍历推送模板信息
	 * */
	public function send_info2(){
		set_time_limit(0);
		$user_id = $_POST["user_ids"];
		if(!empty($user_id)){
			$map["user_id"] = array("in", $user_id);
			$list = D("user")->where($map)->select();
			foreach ($list as $key=>$item){
				$this->sendtemplateinfo($item["domain"], "@".$item["nickname"]."  ".$_POST["title"], $_POST["remark"], $_POST["novel_info"], $_POST["update_date"], $_POST["novel_url"], $item["open_id"]);
			}
		}
		else{
			$list = D("user")->select();
			foreach ($list as $key=>$item){
				$this->sendtemplateinfo($item["domain"], "@".$item["nickname"]."  ".$_POST["title"], $_POST["remark"], $_POST["novel_info"], $_POST["update_date"], $_POST["novel_url"], $item["open_id"]);
			}
		}
		
		echo "the end";
	}
	
	/**
	 * 推送模板信息
	 * */
	protected function sendtemplateinfo($domain, $title, $remark, $novel_info, $update_date, $novel_url, $open_id){		
		$map["domain"] = $domain;
		$platform = D("platform")->where($map)->find();
		if(intval($platform["out_time"]) > time())	{
			$token = getWeChatToken($platform["appid"],$platform["secret"],$platform["token"]);
		}
		else{
			$token = getWeChatToken($platform["appid"],$platform["secret"]);
		}
		//$token = "uE8o4BowpwHdc732mj869AkKGtrVB0Dumi1Hyu_ifLeSVNILA9Yyt1ZfXKJ8P3FpqPk7zYTEKUwD29O4gTAcoVs_NlY070ytewXeqran8iXXGeMLuzug58Sn3X9H2qVJHWXaACAVKL";
		//$openid = "ols0swsTiYZAdF6cdXxvTJqOWWCk"; 
		
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$token";
		$data = '{
		           "touser":"'.$openid.'",
		           "template_id":"MXaFiyY1-r34z4MRJT5qeR5iCao5vCJBnE1HgDREn14",
		           "url":"'.$novel_url.'",       
		           "data":{
	                   "first": {
	                       "value":"'.$title.'",
	                       "color":"#173177"
	                   },
	                   "keyword1":{
	                       "value":"'.$novel_info.'",
	                       "color":"#173177"
	                   },
	                   "keyword2": {
	                       "value":"'.$update_date.'",
	                       "color":"#173177"
	                   },
	                   "remark":{
	                       "value":"'.$remark.'",
	                       "color":"#173177"
	                   }
		           }
				}';
		$result = weChatCurl($url, 1, $data);
		//print_r($result);
	}
}